# -*- coding: utf-8 -*-
from itertools import chain

from odoo import api, fields, models, tools, _
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_repr

from odoo.addons import decimal_precision as dp

from odoo.tools import pycompat

class PricelistItem(models.Model):
    _inherit = "product.pricelist.item"
    _order = "default_code"

    multiple_unit= fields.Float('Multiple_unit',compute='_get_pricelist_item_name_price')
    default_code=fields.Char('描述',related='product_tmpl_id.default_code',store=True)
    default_code2=fields.Char('料号',related='product_tmpl_id.default_code2',store=True)
    categ_id = fields.Many2one(string='Product Category', related='product_tmpl_id.categ_id',store=True)
    list_price = fields.Float('Sale Price', digits=dp.get_precision('Product Price'),related='product_tmpl_id.list_price')
   

    @api.one
    @api.depends('categ_id', 'product_tmpl_id', 'product_id', 'compute_price', 'fixed_price', \
        'pricelist_id', 'percent_price', 'price_discount', 'price_surcharge')
    def _get_pricelist_item_name_price(self):
        if self.categ_id:
            self.name = _("Category: %s") % (self.categ_id.name)
        elif self.product_tmpl_id:
            self.name = self.product_tmpl_id.display_name #dbh
        elif self.product_id:
            self.name = self.product_id.display_name.replace('[%s]' % self.product_id.code, '')
        else:
            self.name = _("All Products")

        if self.compute_price == 'fixed':
            decimal_places = self.env['decimal.precision'].precision_get('Product Price')
            self.price = ("%s %s") % (
                float_repr(
                    self.fixed_price,
                    decimal_places,
                ),
                self.pricelist_id.currency_id.name
            )
            #dbh:
            self.multiple_unit=self.product_tmpl_id.list_price and self.fixed_price/self.product_tmpl_id.list_price
        elif self.compute_price == 'percentage':
            self.price = _("%s %% discount") % (self.percent_price)
        else:
            self.price = _("%s %% discount and %s surcharge") % (self.price_discount, self.price_surcharge)


# class Pricelist(models.Model):
#     _inherit = "product.pricelist"
#     _description = "Pricelist"
#     _order = "sequence asc, id desc"

#     categ_id = fields.Many2one('product.category', string='Product Category')

#     @api.onchange('categ_id')
#     def _onchange_categ_id(self):
#         self.item_ids=self.env['product.pricelist.item'].search([('categ_id','=',self.categ_id.id)])
    